/* 
 * \brief Manages all player entities within the game
 * Can create player objects within the world
 */

using System;
using UnityEngine;

using System.Collections.Generic;

namespace KaoticSSB.Players
{
	public class PlayerManager : MonoBehaviour
	{
		public Rigidbody2D PlayerBase;
		public Rigidbody2D BotBase;

		public PlayerManager ()
		{
		}

		public Player GetPlayer(int playerIndex)
		{
			Player[] players = GameObject.FindObjectsOfType<Player>();
			foreach(Player player in players)
			{
				if(player.PlayerIndex == playerIndex)
				{
					return player;
				}
			}
			return null;
		}

		public Rigidbody2D CreatePlayer(int playerIndex)
		{
			if(playerIndex > 4)
			{
				throw new Exception("More than 4 players not currently supported");
			}

			Rigidbody2D gameObj = Instantiate(PlayerBase, new Vector3(0,0,0), Quaternion.identity) as Rigidbody2D;
			Player player = gameObj.GetComponent<Player>();
			player.SetIndex(playerIndex);
			player.Respawn();
			return gameObj;
		}

		public Rigidbody2D CreateBot(int playerIndex)
		{
			if(playerIndex > 4)
			{
				throw new Exception("More than 4 players not currently supported");
			}
			
			Rigidbody2D gameObj = Instantiate(BotBase, new Vector3(0,0,0), Quaternion.identity) as Rigidbody2D;
			BotBase bot = gameObj.GetComponent<BotBase>();
			bot.SetIndex(playerIndex);
			bot.Respawn();
			return gameObj;
		}
	}
}

